﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using Microsoft.SqlServer.Management.Common;
using Microsoft.SqlServer.Management.Smo;
using System.Data.SqlClient;

using System.IO;

using MM.Entities;
using MM.Business;


namespace MM
{
    public partial class frmExecuteSQL : Form
    {
        private string sSQL = "";
        private string _sConn = "";

        public frmExecuteSQL(string strConn)
        {
            InitializeComponent();
            _sConn = strConn;
        }

        private void cmdBrowse_Click(object sender, EventArgs e)
        {
            OpenFileDialog f = new OpenFileDialog();
            f.Filter = "Text file (*.sql*)|*.sql*";
            f.Title = "CHỌN FILE ";
            if (f.ShowDialog() == DialogResult.OK)
            {
               
                this.txtFileName.Text = f.FileName;
                this.DocNoiDungFile(f.FileName);
            }
        }

        private void DocNoiDungFile(string filename)
        {
            try
            {
                FileInfo file = new FileInfo(filename);

                sSQL = file.OpenText().ReadToEnd();

                if (sSQL == "")
                {
                    MessageBox.Show("KHÔNG THỂ LOAD NỘI DUNG FILE");
                    this.cmdThucThi.Enabled = false;
                }
                else
                {
                    this.cmdThucThi.Enabled = true;
                    this.txtNoidung.Text = sSQL;
                }
                file.OpenText().Close();
            }
            catch(Exception ex)
            {
                MessageBox.Show("LỖI: "+ ex.Message);
            }
        }

        private void  cmdThucThi_Click(object sender, EventArgs e)
        {
            int kq = -1;
            string ERR = "";
            try
            {
                try
                {
                    SqlHelper.ConnectionString = _sConn;
                    SqlHelper.OpenConnection();
                 

                    

                    Server server = new Server(new ServerConnection(SqlHelper.Connection));

                    server.ConnectionContext.ExecuteNonQuery(sSQL);
                    

                    kq = 1;
                   
                }
                catch (Exception ex)
                {
                    kq = -1;
                    ERR = ex.Message;
                }
                finally
                {
                    if (!SqlHelper.IsHaveTran())
                        SqlHelper.CloseConnection();
                }

                if (kq == -1)
                    MessageBox.Show("LỖI: " + ERR);
                else
                    MessageBox.Show("CẬP NHẬT THÀNH CÔNG");
            }
            catch
            {
            }
        }

        private void frmExecuteSQL_Load(object sender, EventArgs e)
        {
            Common.SetColor(this);
        }
    }
}